<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <title>添加分类表</title>
  <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-row">
    <div class="layui-form layui-form-pane" lay-filter="categoryForm" id="categoryForm">
        <div class="layui-form-item">
            <label class="layui-form-label">图标</label>
            <div class="layui-input-inline">
                <div class="layui-upload">
                    <input type="hidden" id="icon" name="icon">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" onclick="fun.preview(this)" id="upload-img">
                        <p id="upload-text"></p>
                    </div>
                    <button type="button" class="layui-btn layui-btn-radius layui-btn-primary" id="upload-btn">上传</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">名称</label>
          <div class="layui-input-block">
            <input type="text" lay-verify="required"  class="layui-input" name="name" placeholder="请输入名称"/>
          </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">上级分类</label>
                <div class="layui-input-inline">
                    <div class="layui-row">
                        <div class="layui-col-md9">
                            <input name="parentId" type="hidden" lay-verify="required">
                            <input name="parentName" type="text" class="layui-input layui-disabled" placeholder="请选择分类" disabled>
                        </div>
                        <div class="layui-col-md3">
                            <button type="button" class="layui-btn layui-btn-primary categoryWizard">选择</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">顺序</label>
                <div class="layui-input-inline">
                    <input type="number" lay-verify="required"  class="layui-input" name="orderNum" placeholder="请输入顺序"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remark" placeholder="请输入备注"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">创建者</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input layui-disabled" name="createBy" disabled="disabled"/>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">创建时间</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input layui-disabled" name="createTime" disabled="disabled"/>
                </div>
            </div>
        </div>
      <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="save-submit" id="save-submit" value="确认">
      </div>
    </div>
  </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
  layui.config({
      base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
     index: 'lib/index', //主入口模块
  }).use(['index', 'form','crud','treeSelect','upload'], function(){
    let $ = layui.$,
            form = layui.form,
            crud = layui.crud,
        upload = layui.upload,
        treeSelect= layui.treeSelect;
    form.val('categoryForm', {
        parentId: [[${parentId}]],
        parentName: [[${parentName}]]
    });
    form.on('submit(save-submit)', function(data) {
      $.ajax({
        type: 'POST',
        url:  ctx + '/app/category/add',
        data: JSON.stringify(data.field),
        contentType:'application/json;charset=UTF-8',
        dataType: 'json',
        success: function(result) {
          layer.msg(result.message);
          if (result.code === 200) {
            let index = parent.layer.getFrameIndex(window.name);
            // 关闭
            parent.layer.close(index);
            // 刷新
            parent.location.reload();
          }
        }
      });
    });
    //普通图片上传
    crud.uploadImg("icon");
    // 分类向导
    $('.categoryWizard').on('click', function () {
        // 树结构向导
        crud.treeWizard("/app/category/treeSelect",function (result) {
            form.val('categoryForm', {
                parentId: result.id,
                parentName: result.name
            });
        });
    });
  });
</script>
</body>
</html>